package hcmus.fit.master.sevensea.background;

import hcmus.fit.master.sevensea.core.AbstractLayer;
import hcmus.fit.master.sevensea.util.Size;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;

public class WaveLayer extends AbstractLayer {
    private Point deltaPos;
    public WaveLayer(Size viewSize) {
        super(viewSize);
        this.deltaPos = new Point(0, 0);
    }

    @Override
    public void draw(Canvas canvas) {
        Bitmap currentBitmap = this.source;
        int srcW = currentBitmap.getWidth();
        int srcH = currentBitmap.getHeight();
        int viewW = this.viewSize.getWidth();
        int viewH = this.viewSize.getHeight();
        int srcX = srcW - deltaPos.x;
        int srcY = srcH - deltaPos.y;
        int sizeX = viewW - deltaPos.x;
        int sizeY = viewH - deltaPos.y;
        // Part 1
        Rect s = new Rect(srcX, srcY, srcW, srcH);
        Rect d = new Rect(0, 0, deltaPos.x, deltaPos.y);
        canvas.drawBitmap(currentBitmap, s, d, this.paint);

        // Part 2
        s = new Rect(0, srcY, sizeX, srcH);
        d = new Rect(deltaPos.x, 0, viewW, deltaPos.y);
        canvas.drawBitmap(currentBitmap, s, d, this.paint);

        // Part 3
        s = new Rect(0, 0, sizeX, sizeY);
        d = new Rect(deltaPos.x, deltaPos.y, viewW, viewH);
        canvas.drawBitmap(currentBitmap, s, d, this.paint);

        // Part 4
        s = new Rect(srcX, 0, srcW, sizeY);
        d = new Rect(0, deltaPos.y, deltaPos.x, viewH);
        canvas.drawBitmap(currentBitmap, s, d, this.paint);
    }

    @Override
    public boolean update() {
        int x = (deltaPos.x + 1) % this.source.getWidth();
        int y = (deltaPos.y + 1) % this.source.getHeight();
        deltaPos.set(x, y);
        return true;
    }

    @Override
    public Bitmap getSourceFromResource() {
        return WaveLayer.resources;
    }

}
